package com.interview.service.impl;


import com.interview.entity.ExameeDrawLots;

import com.interview.entity.SignedInfo;
import com.interview.entity.SignedWithDrawLot;
import com.interview.mapper.DrawLotsMapper;

import com.interview.mapper.SignedInfoMapper;
import com.interview.mapper.SignedWithDrawLotMapper;
import com.interview.service.ScheduleService;
import com.interview.service.DrawLotsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DrawLotsServiceImpl implements DrawLotsService {


    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private DrawLotsMapper drawLotsMapper;
    @Autowired
    private SignedInfoMapper signedInfoMapper;
    @Autowired
    private SignedWithDrawLotMapper signedWithDrawLotMapper;


    @Override
    public ExameeDrawLots drawLotsOneByOne(String idNumber) {
        //获取签到考生的信息
        SignedInfo signedInfo = signedInfoMapper.selectById(idNumber);
        //获取该组的所有待抽签数据
        String groupId = signedInfo.getGroupId();
        List<String> drawLotsAwaitList = scheduleService.getDrawLotsAwaitList(groupId);
        //获取一个随机下标
        int index = (int)Math.floor(Math.random() * drawLotsAwaitList.size());
        //获取随机下标对应的数字
        String number = drawLotsAwaitList.get(index);
        ExameeDrawLots drawLots = new ExameeDrawLots(idNumber, groupId, number);
        return drawLots;
    }

    @Override
    public void savaDrawLotsResult(ExameeDrawLots drawLots) {
        drawLotsMapper.insert(drawLots);
    }

    @Override
    public String getDrawLotNumberByIdNumber(String idNumber) {
        ExameeDrawLots drawLots = drawLotsMapper.selectById(idNumber);
        if (drawLots != null) {
            return drawLots.getDrawLotsNumber();
        }else {
            return "";
        }
    }

    @Override
    public List<SignedWithDrawLot> getDrawLotNumberWithSignedInfo() {
        List<SignedWithDrawLot> signedWithDrawLots = signedWithDrawLotMapper.selectList(null);
        return signedWithDrawLots;
    }

    @Override
    public List<ExameeDrawLots> getDrawLotsList() {
        List<ExameeDrawLots> drawLotsList = drawLotsMapper.selectList(null);
        return drawLotsList;
    }


}
